package com.qqtech.qquan.community.controller;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qqtech.core.common.annotation.VisitAnnt;
import com.qqtech.core.common.enums.RespEnum;
import com.qqtech.core.common.enums.YesNoEnum;
import com.qqtech.core.common.util.TimeUtil;
import com.qqtech.core.frame.model.Result;
import com.qqtech.core.frame.service.BaseService;
import com.qqtech.qquan.community.model.CommunityBaseVo;
import com.qqtech.qquan.community.model.CommunitySign;
import com.qqtech.qquan.community.model.CommunitySignQuery;
import com.qqtech.qquan.community.service.CommunityBaseService;
import com.qqtech.qquan.community.service.CommunityJoinService;
import com.qqtech.qquan.community.service.CommunitySignService;
import com.qqtech.qquan.hi.enums.HiRuleClassifyDictEnum;
import com.qqtech.qquan.hi.enums.HiRuleTaskTypeDictEnum;
import com.qqtech.qquan.hi.model.HiRuleQuery;
import com.qqtech.qquan.hi.model.HiRuleResultQuery;
import com.qqtech.qquan.hi.model.HiRuleResultVo;
import com.qqtech.qquan.hi.model.HiRuleVo;
import com.qqtech.qquan.hi.service.HiCommunityService;
import com.qqtech.qquan.hi.service.HiRuleResultService;
import com.qqtech.qquan.hi.service.HiRuleService;
import com.qqtech.qquan.user.model.UserBaseVo;
import com.qqtech.qquan.user.service.UserBaseService;
import com.qqtech.web.context.BaseWebController;

@Controller
@RequestMapping(value = "/community/sign")
public class CommunitySignController extends BaseWebController<CommunitySign, CommunitySignQuery> {
	@Resource
	private CommunitySignService communitySignService;

	@Resource
	private CommunityBaseService communityBaseService;

	@Resource
	private CommunityJoinService communityJoinService;

	@Resource
	private HiCommunityService hiCommunityService;

	@Resource
	private HiRuleResultService hiRuleResultService;

	@Resource
	private HiRuleService hiRuleService;

	@Resource
	private UserBaseService userBaseService;

	@Override
	protected BaseService<CommunitySign> getBaseService() {
		return communitySignService;
	}

	/**
	 * 新增
	 * 
	 * @param add
	 * @return
	 */
	@VisitAnnt(type = RespEnum.LOGIN)
	@RequestMapping(value = "/add")
	@ResponseBody
	public Result add() {
		Result result = new Result();
		Integer userId = super.getUserId();
		UserBaseVo userBaseVo=userBaseService.getById(userId);
		int isSign=2;
		if(null!=userBaseVo.getCommunityId()){
			CommunitySignQuery communitySignQuery = new CommunitySignQuery();
			communitySignQuery.setUserId(userId);
			communitySignQuery.setNewAt(TimeUtil.now());
			int count = communitySignService.queryCount(communitySignQuery);
			if (count > 0) {
				result.addErr("您今天已签到过社区了。");
				return result;
			}
			CommunitySign communitySign=new CommunitySign();
			communitySign.setUserId(userId);
			communitySign.setCommunityId(userBaseVo.getCommunityId());
			communitySignService.insert(communitySign);
			isSign=1;
			// 荣誉签到-ryz
			hiCommunityService.sign(userId, userBaseVo.getCommunityId());
			CommunityBaseVo communityBaseVo = communityBaseService.getById(userBaseVo.getCommunityId());
			result.put("communityBaseVo", communityBaseVo);
		}else{
			result.addErr("您未加入社区。");
			return result;
		}
		result.put("isSign", isSign);
		return result;
	}

	/**
	 * 获取签到
	 * 
	 * @param add
	 * @return
	 */
	@VisitAnnt(type = RespEnum.LOGIN)
	@RequestMapping(value = "/detail")
	@ResponseBody
	public Result detail(@RequestBody CommunitySign communitySign) {
		Result result = new Result();
		Integer userId = super.getUserId();
		UserBaseVo user = userBaseService.getById(userId);
		int joinCommunity = 2;
		int signCommunity = 2;
		// 累计签到次数
		int times = 1;
		// 累计签到荣誉
		int totalHonor = 0;
		// 累计签到圈币
		int totalQuan = 0;
		if (null != user.getCommunityId()) {
			CommunitySignQuery communitySignQuery = new CommunitySignQuery();
			communitySignQuery.setUserId(userId);
			communitySignQuery.setNewAt(TimeUtil.now());
			int count = communitySignService.queryCount(communitySignQuery);
			if (count >0) {
				signCommunity = 1;
			}
			joinCommunity = 1;
			HiRuleResultQuery query = new HiRuleResultQuery();
			query.setUserId(userId);
			query.setClassify(HiRuleClassifyDictEnum.SIGN.getCode());
			// 累计签到荣誉
			totalHonor = hiRuleResultService.queryTotalHonor(query);
			// 累计签到圈币
			totalQuan = hiRuleResultService.queryTotalQuan(query);

			HiRuleResultVo hiRuleResultNewest = hiRuleResultService.getNewest(userId,
					HiRuleClassifyDictEnum.SIGN.getCode(), user.getCommunityId());
			if (hiRuleResultNewest == null) { // 没有数据，表示首次签到
				times = 1;
			} else {
				if (isLastDay(hiRuleResultNewest.getUpdAt())) {// 上次记录是昨天
					times = hiRuleResultNewest.getTimes() + 1;
				} else if (isToday(hiRuleResultNewest.getUpdAt())) { // 上次记录是今天
					times = hiRuleResultNewest.getTimes() + 1;
				} else {
					// 时间间断了
					times = 1;
				}
			}
		}
		HiRuleQuery hiRuleQuery = new HiRuleQuery();
		hiRuleQuery.setClassify(HiRuleClassifyDictEnum.SIGN.getCode());
		hiRuleQuery.setStatus(YesNoEnum.YES.getCode());
		hiRuleQuery.setTaskType(HiRuleTaskTypeDictEnum.CONTINUITY.getCode());
		List<HiRuleVo> list = hiRuleService.queryList(hiRuleQuery);
		result.put("communityId", user.getCommunityId());
		result.put("signCommunity", signCommunity);
		result.put("list", list);
		result.put("times", times);
		result.put("totalHonor", totalHonor);
		result.put("totalQuan", totalQuan);
		result.put("joinCommunity", joinCommunity);
		return result;
	}

	/**
	 * 判断是否为今天
	 * 
	 * @param updateTime
	 *            输入的时间
	 * @return
	 */
	private boolean isToday(Timestamp updateTime) {
		Calendar cd = Calendar.getInstance();
		Date date1 = cd.getTime();
		Date date2 = updateTime;
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		if (sdf.format(date1).equals(sdf.format(date2))) {
			return true;
		}
		return false;
	}

	/**
	 * 判断是否为昨天
	 * 
	 * @param updateTime
	 *            输入的时间
	 * @return
	 */
	private boolean isLastDay(Timestamp updateTime) {
		Calendar cd = Calendar.getInstance();
		cd.add(Calendar.DATE, -1);
		Date date1 = cd.getTime();
		Date date2 = updateTime;
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		if (sdf.format(date1).equals(sdf.format(date2))) {
			return true;
		}
		return false;
	}
}
